//
//  XTCollectionViewCell.m
//  XTNews
//
//  Created by tage on 14-4-30.
//  Copyright (c) 2014年 XT. All rights reserved.
//

#import "XTCollectionViewCell.h"
#import "PicModel.h"

@interface XTCollectionViewCell ()
@property (nonatomic, retain) UIImageView *imageView;
@property (nonatomic, retain) UIImageView *numImage;
@property (nonatomic, retain) UILabel     *numLabel;
@end

@implementation XTCollectionViewCell


- (void)dealloc {
    RELEASE_SAFELY(_picture);
    RELEASE_SAFELY(_imageView);
    RELEASE_SAFELY(_numImage);
    RELEASE_SAFELY(_numLabel);
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:CGRectMake(0, 0, kDefaultPictureCellWidth, kDefaultPictureCellHeight)];
    if (self)
        {

        }
    return self;
}

- (void)prepareForReuse
{
    [super prepareForReuse];
    //    RELEASE_SAFELY(_picture);
}

- (void)collectionView:(PSCollectionView *)collectionView fillCellWithObject:(id)object atIndex:(NSInteger)index {
    [super collectionView:collectionView fillCellWithObject:object atIndex:index];
    self.collectionView = collectionView;
    self.object = object;
    self.picture = object;
    self.index = index;
}

+ (CGFloat)rowHeightForObject:(id)object inColumnWidth:(CGFloat)columnWidth {
    return kDefaultPictureCellHeight;
}


- (void)setPicture:(PicModel *)picture{
    if (_picture != picture) {
        _picture = picture;
    }
    NSString *url = nil;
    if (_picture.thumal) {
        url = _picture.thumal;
        __block UIImageView *wImageView = self.imageView;
        [self.imageView setImageWithURL:[NSURL URLWithString:url] placeholderImage:IMGNAMED(@"emptyLarge.png") completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType) {
            if (image) {

//                CABasicAnimation *opAnim = [CABasicAnimation animationWithKeyPath:@"opacity"];
//                opAnim.duration = .25;
//                opAnim.fromValue = [NSNumber numberWithFloat:0.0];
//                opAnim.toValue = [NSNumber numberWithFloat:1.0];
//                opAnim.removedOnCompletion = YES;
//                [opAnim setValue:@"fadeInAnim" forKey:@"moveAnim"];
//                opAnim.fillMode = kCAFillModeForwards;
//
//                [wImageView.layer addAnimation:opAnim forKey:@"moveAnim"];
            }

        }];
    }

    if ([_picture.SinglePics count] > 0)
        {

        self.numLabel.text = [NSString stringWithFormat:@"%d",[_picture.SinglePics count]];
        }else
            {
            self.numLabel.text = [NSString stringWithFormat:@"%d",0];
            }
}
- (UIImageView *)imageView{
    if (!_imageView) {
        _imageView = [[UIImageView alloc] initWithFrame:self.bounds];
        _imageView.backgroundColor = [UIColor clearColor];
        _imageView.clipsToBounds = YES;
        _imageView.contentMode = UIViewContentModeScaleAspectFill;
        [_imageView addSubview:self.numImage];
        [_imageView addSubview:self.numLabel];
        [self addSubview:_imageView];
    }
    return _imageView;
}

- (UIImageView *)numImage
{
    if (!_numImage)
        {
        _numImage = [[UIImageView alloc] initWithImage:IMGNAMED(@"bg_imagecount.png")];
        _numImage.frame = CGRectMake(self.bounds.size.width - 20,
                                     self.bounds.size.height - 20,
                                     15,
                                     15);
        }
    return _numImage;
}

- (UILabel *)numLabel
{
    if (!_numLabel)
        {
        _numLabel = [[UILabel alloc] initWithFrame:self.numImage.frame];
        _numLabel.backgroundColor = [UIColor clearColor];
        _numLabel.font = FONT_NUMBER(11.0f);
        _numLabel.textAlignment = NSTextAlignmentCenter;
        _numLabel.textColor = [UIColor whiteColor];
        }
    return _numLabel;
}

@end
